package com.example.myapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class List1Activity extends AppCompatActivity {

    RecyclerView recyclerView;
    FloatingActionButton add_button;
    MyDatabaseHelper myDB;
    ArrayList<String> my_id,my_age,my_height,my_weight,my_sex,my_type;
    CustomAdapter customAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list1);

        recyclerView = findViewById(R.id.recyclerview);
        add_button = findViewById(R.id.add_button);
        //浮框按钮
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(List1Activity.this, InputStateActivity.class);
                startActivity(intent);
            }
        });


        myDB = new MyDatabaseHelper(List1Activity.this);
        my_id = new  ArrayList<>();
        my_age = new ArrayList<>();
        my_height = new ArrayList<>();
        my_weight = new ArrayList<>();
        my_sex = new ArrayList<>();
        my_type = new ArrayList<>();

        //调用方法
        storeDataInArrays();//从数据库中获取值
      //下方菜单栏

        //用adapter连接
        customAdapter = new CustomAdapter(List1Activity.this,this,my_id,my_age,my_height
                ,my_weight,my_sex,my_type);
        recyclerView.setAdapter(customAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(List1Activity.this));




        ImageButton list_my = findViewById(R.id.listmy);
        ImageButton list_add = findViewById(R.id.listadd);
        ImageButton list_home = findViewById(R.id.listhome);

        list_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent home1 = new Intent(List1Activity.this, List1Activity.class);//跳转到的页面，必备
                startActivityForResult(home1,4);

            }
        });
        list_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent home1 = new Intent(List1Activity.this, InputStateActivity.class);//跳转到的页面，必备
                startActivityForResult(home1,4);

            }
        });
        list_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent home1 = new Intent(List1Activity.this, MainActivity.class);//跳转到的页面，必备
                startActivityForResult(home1,4);

            }
        });
    }






    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            recreate();
        }
    }

    //将数据库数据放入列表
    void storeDataInArrays(){
        //读取数据库中的所有数据
        Cursor cursor = myDB.readAllData();
        if(cursor.getCount()==0){
            Toast.makeText(this,"No data",Toast.LENGTH_SHORT).show();
        }
        else {
            //指针每移动一行，就加一行列表（将值全部加进去）
            while (cursor.moveToNext()){
                my_id.add(cursor.getString(0));
                my_age.add(cursor.getString(1));
                my_height.add(cursor.getString(2));
                my_weight.add(cursor.getString(3));
                my_sex.add(cursor.getString(4));
                my_type.add(cursor.getString(5));

            }
        }

    }

    //上方菜单栏的创建
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

   //选择功能进行事件处理
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()== R.id.delete_all){
            confirmDialog();
        }
        if (item.getItemId()== R.id.search){
            Intent home1 = new Intent(List1Activity.this, Health_homeActivity.class);//跳转到的页面，必备
            startActivityForResult(home1,4);
        }
        return super.onOptionsItemSelected(item);
    }

    //删除功能
    void confirmDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("删除所有");
        builder.setMessage("确定要删除所有数据吗?");
        builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                MyDatabaseHelper myDB = new MyDatabaseHelper(List1Activity.this);
                myDB.deleteAllData();
                Toast.makeText(List1Activity.this,"删除所有成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(List1Activity.this, List1Activity.class);
                startActivity(intent);
                finish();
            }
        });
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        builder.create().show();
    }
}
